package com.project.dao.impl;

import com.project.bean.TradeBean;
import com.project.dao.ITradeDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TradeDaoImpl extends BaseDao implements ITradeDao {
    @Override
    public void addInfo(TradeBean tradeBean) {
        this.setConnection();
        try {
            this.ps = connection.prepareStatement("insert into t_info(i_time,i_info,i_account,fk_user)values (?,?,?,?)");
            this.ps.setObject(1,tradeBean.getTime());
            this.ps.setObject(2, tradeBean.getDescrible());
            this.ps.setObject(3, tradeBean.getAccount());
            this.ps.setObject(4, tradeBean.getUserBean().getUserName());
            this.ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public List<TradeBean> findByCode(String code) {
        List list = new ArrayList();
        this.setConnection();
        try {
            this.ps = this.connection.prepareStatement("select * from t_info where fk_user = ? ");
            this.ps.setObject(1, code);
            this.rs = this.ps.executeQuery();
            while (this.rs.next()) {
                TradeBean tradeBean = new TradeBean(this.rs.getString("i_info"),this.rs.getDouble("i_account")
                ,this.rs.getString("fk_user"));
                list.add(tradeBean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }

        return (List<TradeBean>)list;
    }

}


